package com.itcast.tanhuasso.controller;

import com.itcast.tanhuasso.service.SmsService;
import com.itcast.tanhuasso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuasso.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/721:29
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@Slf4j
public class SmsController{

    @Autowired
    private SmsService smsService;

    @PostMapping(value = "login")
    public ResponseEntity<ErrorResult> sendYzmCode(@RequestBody Map<String,String> parm){
        ErrorResult errorResult = null;
        String phone = parm.get("phone");
        try {
            errorResult = smsService.checkCode(phone);
            if (errorResult == null){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("发送短息验证码失败 ~ phone : " + phone);
            errorResult = ErrorResult.builder().errCode("000002").errMessage("短息验证码发送失败 ~ phone : " + phone).build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
